﻿using Resn.WhuPub.BIL;
using Resn.WhuPub.DML;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Resn.WhuPub.DAL;

namespace Resn.WhuPub.BLL
{
        /// <summary>
        /// 实现经销商管理器的属性和方法
        /// </summary>
        public class DealerManager : IDealerManager
        {
                #region 属性

                private static DealerManager _instance;

                /// <summary>
                /// 获取经销商管理器实例
                /// </summary>
                /// <value>
                /// 经销商管理器实例
                /// </value>
                public static DealerManager Instance
                {
                        get { return _instance ?? (_instance = new DealerManager()); }
                }

                /// <summary>
                /// 获取所有经销商集合
                /// </summary>
                /// <returns>
                /// 所有经销商集合
                /// </returns>
                public List<IDealer> Dealers
                {
                        get
                        {
                                var models = DBStorage.Instance.Select<DealerModel>();

                                var dealers = new List<IDealer>();

                                models.ForEach(i => dealers.Add(new Dealer(i)));

                                return dealers;
                        }
                } 
                #endregion

                #region 方法

                /// <summary>
                /// 创建经销商
                /// </summary>
                /// <param name="name">名称</param>
                /// <param name="province">省份</param>
                /// <param name="city">城市</param>
                /// <param name="Postcode">邮编</param>
                /// <param name="registerAddress">注册地址</param>
                /// <param name="businessAddress">经营地址</param>
                /// <param name="contactName">联系人姓名</param>
                /// <param name="contactTelephone">联系人电话</param>
                /// <param name="contactFax">联系人传真</param>
                /// <param name="contactEmail">联系人电子邮箱</param>
                /// <returns>
                /// 返回创建的经销商对象
                /// </returns>
                public IDealer CreateDealer(string name, string province, string city, string Postcode, string registerAddress,
                                            string businessAddress, string contactName, string contactTelephone, string contactFax,
                                            string contactEmail)
                {
                        var model = new DealerModel
                                {
                                        Id = Guid.NewGuid().ToString(),
                                        Name = name,
                                        Province = province,
                                        City = city,
                                        Postcode = Postcode,
                                        RegisterAddress = registerAddress,
                                        BusinessAddress = businessAddress,
                                        ContactName = contactName,
                                        ContactTelephone = contactTelephone,
                                        ContactFax = contactFax,
                                        ContactEmail = contactEmail
                                };

                        return  new Dealer(model);
                }

                #endregion
        }
}